package com.intelligent.system.common.websocket;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class WebSocketUtils {
    private final WebSocketHandler webSocketHandler;

    /**
     * 发送私聊消息
     */
    public void sendPrivateMessage(String from, String to, String content) {
        WebSocketMessage message = new WebSocketMessage("private", from, to, content);
        webSocketHandler.sendPrivateMessage(message);
    }

    /**
     * 发送广播消息
     */
    public void broadcastMessage(String from, String content) {
        WebSocketMessage message = new WebSocketMessage("broadcast", from, null, content);
        webSocketHandler.broadcastMessage(message);
    }

    /**
     * 发送系统消息
     */
    public void sendSystemMessage(String to, String content) {
        WebSocketMessage message = new WebSocketMessage("system", "system", to, content);
        webSocketHandler.sendPrivateMessage(message);
    }

    /**
     * 获取在线用户数
     */
    public int getOnlineCount() {
        return webSocketHandler.getSessions().size();
    }
}